//
//  Main.cpp
//  BaseDemo
//
//  Created by jiuyv on 2019/5/8.
//  Copyright © 2019 Julija. All rights reserved.
//

#include "GLTools.h"
#include <glut/glut.h>
#include <stdio.h>

GLBatch    squareBatch;
GLShaderManager shaderManager;

void ChangeSize(int w, int h){
    glViewport(0, 0, w, h);
}

GLfloat blockSize = 0.2f;
GLfloat vVerts[] = { -blockSize, -blockSize, 0.0f,
    blockSize, -blockSize, 0.0f,
    blockSize,  blockSize, 0.0f,
    -blockSize,  blockSize, 0.0f};

void SetupRC(){
    
    shaderManager.InitializeStockShaders();
    
    //绘制一个移动矩形
    squareBatch.Begin(GL_TRIANGLE_FAN, 4);
    squareBatch.CopyVertexData3f(vVerts);
    squareBatch.End();
    
}

void RenderScene(void){
    
    //设置清屏颜色
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    
    //清除一个或一组特定缓冲区
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    
    //设置剪裁区颜色为红色
    glClearColor(1, 0, 0, 1);
    //设置裁剪尺寸
    glScissor(100, 100, 200, 200);
    //开启裁剪测试
    glEnable(GL_SCISSOR_TEST);
    //开启清屏,执行裁剪
    glClear(GL_COLOR_BUFFER_BIT);
    
    //黑色
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glScissor(100, 300, 200, 200);
    glClear(GL_COLOR_BUFFER_BIT);
    
    //裁剪一个绿色的矩形
    glClearColor(0, 1, 0, 1);
    glScissor(300, 300, 200, 200);
    glClear(GL_COLOR_BUFFER_BIT);
    
    //蓝色
    glClearColor(0, 0, 1, 1);
    glScissor(300, 100, 200, 200);
    glClear(GL_COLOR_BUFFER_BIT);
    
    //关闭裁剪测试
    glDisable(GL_SCISSOR_TEST);
    
    
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 0.5f };
    //开启混合
    glEnable(GL_BLEND);
    //开启组合函数,计算混合颜色因子
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    //使用着色器管理器
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
    //容器类开始绘制
    squareBatch.Draw();
    
    //关闭混合功能
    glDisable(GL_BLEND);
    
    //将在后台缓冲进行渲染,结束时交换到前台
    glutSwapBuffers();
    
}
void SpecialKeys(int key, int x, int y){
    
    GLfloat stepSize = 0.025f;
    
    GLfloat blockX = vVerts[0];
    GLfloat blockY = vVerts[7];
    
    if (key == GLUT_KEY_UP) {
        blockY += stepSize;
    }
    if (key == GLUT_KEY_DOWN) {
        blockY -= stepSize;
    }
    if (key == GLUT_KEY_LEFT) {
        blockX -= stepSize;
    }
    if (key == GLUT_KEY_RIGHT) {
        blockX += stepSize;
    }
    
    if(blockX < -1.0f) blockX = -1.0f;
    if(blockX > (1.0f - blockSize * 2)) blockX = 1.0f - blockSize * 2;;
    if(blockY < -1.0f + blockSize * 2)  blockY = -1.0f + blockSize * 2;
    if(blockY > 1.0f) blockY = 1.0f;
    
    vVerts[0] = blockX;
    vVerts[1] = blockY - blockSize*2;
    
    vVerts[3] = blockX + blockSize*2;
    vVerts[4] = blockY - blockSize*2;
    
    vVerts[6] = blockX + blockSize*2;
    vVerts[7] = blockY;
    
    vVerts[9] = blockX;
    vVerts[10] = blockY;
    
    squareBatch.CopyVertexData3f(vVerts);
    
    glutPostRedisplay();

}


int main(int argc, char* argv[]){
    
    //设置当前工作目录,针对MAC OS X
    gltSetWorkingDirectory(argv[0]);
    
    //初始化GLUT库
    glutInit(&argc, argv);
    
    //初始化缓冲区窗口,其中标志GLUT_DOUBLE GLUT_RGBA GLUT_DEPTH GLUT_STENCIL 分别指 双缓冲区 颜色缓冲区 深度缓冲区 模板缓冲区
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    
    glutInitWindowSize(600, 600);
    
    glutCreateWindow("BaseDemo");
    
    //注册回调函数
    glutReshapeFunc(ChangeSize);
    
    glutDisplayFunc(RenderScene);
    
    glutSpecialFunc(SpecialKeys);
    //驱动程序初始化过程中是否出现问题
    GLenum error = glewInit();
    if (GLEW_OK != error) {
        fprintf(stderr, "glew error:%s\n",glewGetErrorString(error));
        return 1;
    }
    
    SetupRC();
    
    glutMainLoop();
    
    return 0;
}

